package com.umbr3lla.main.state.cards;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class CardUtils {

	
	public static BufferedImage loadImage(String imageName)
	{
		BufferedImage bi=null;
		try {
			bi = ImageIO.read(new File(imageName));
		} catch (IOException e) {
			System.out.println(e.toString());
		}
		
		return bi;
	}
	
	
	public static Image loadImagePak(final String imageName) {
	      final java.lang.ClassLoader loader = Thread.currentThread().getContextClassLoader();
	      java.awt.Image image  = null;
	      java.io.InputStream is = (java.io.InputStream)
	         java.security.AccessController.doPrivileged(
	            new java.security.PrivilegedAction<InputStream>() {
	               public InputStream run() {
	                  if (loader != null) {
	                     return loader.getResourceAsStream(imageName);
	                  } else {
		             return ClassLoader.getSystemResourceAsStream(imageName);
	                  }
	               }
	            });
	         if (is != null) {
	            try {
	               final int BlockLen = 256;
	               int offset = 0;
	               int len;
	               byte imageData[] = new byte[BlockLen];
	               while ((len = is.read(imageData, offset, imageData.length - offset)) > 0) {
	                  if (len == (imageData.length - offset)) {
	                     byte newData[] = new byte[imageData.length * 2];
	                     System.arraycopy(imageData, 0, newData, 0, imageData.length);
	                     imageData = newData;
	                     newData   = null;
	                  } 
	                  offset += len;
	               }
	               image = java.awt.Toolkit.getDefaultToolkit()
	                          .createImage(imageData);
	            } catch (java.io.IOException ex) {}
	         }
	      return image;
	}
	
}
